package xworker.javafx.beans.value;

import javafx.beans.property.Property;
import javafx.beans.value.ObservableValue;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.javafx.beans.property.PropertyFactory;

public class ObservableValueActions {
    public static void createBindToProperty(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        ObservableValue parent = actionContext.getObject("parent");
        if(parent == null){
            throw new NullPointerException("Parent value is null, path=" + self.getMetadata().getPath());
        }

        Object[] properties = self.doAction("getProperties", actionContext);
        if(properties != null){
            for(Object p : properties){
                if(p instanceof Property){
                    Property property = (Property) p;
                    property.bind(parent);
                }else if(p instanceof  String){
                    String str = (String) p;
                    if(str.contains(".")){
                        String[] strs = str.split("[.]");
                        Property property = PropertyFactory.getProperty(actionContext.getObject(strs[0].trim()), strs[1].trim());
                        if(property != null){
                            property.bind(parent);
                        }
                    }
                }
            }
        }
    }
}
